home *** CD-ROM | disk | FTP | other *** search
/ QuickTime for the Web (2nd Edition) / QuickTime for the Web (2nd Edition).iso / pc / Demos / Mac / Matthew's Behaviors / TrigMathƒ / TrigMath1.0 < prev   
Encoding:
INI File  |  2001-09-10  |  6.4 KB  |  209 lines

  1. [Name]
  2. TrigMath v1.0 - Provides events Trig math functions
  3. By Matthew Peterson, matthew@pinoko.berkeley.edu
  4.  
  5. [Description]
  6. 2-19-2000
  7. This behavior offers the following eight functions (Scroll down for a better
  8. description of this behavior):
  9.  
  10. --------------------
  11. To use this behavior: make a new sprite and add this behavior to it.
  12. It doesn't matter what image the sprite has (small non-vector images
  13. are best) nor does it matter where the sprite is placed. 
  14. This sprite will be hidden from view as it is just used as a holder 
  15. for the following math routines:
  16.  
  17. Trig                                                 :        This event calculates the Sin, Cos and Tan for input TrigAngle.
  18.                                                                                 Input     - TrigAngle
  19.                                                                                 Output - sine, cosine, tangent
  20.  
  21. ArcTan                    :        This event performs the Arc Tangent operation.
  22.                                                                                                 Input     - TrigInput
  23.                                                                                                 Output -TrigOutput
  24.  
  25. Polar2Cart        :        This event converts polar to cartesian coordinates.
  26.                                                                                         Input     - TrigAngle, TrigDistance
  27.                                                                                         Output - Trig1X, Trig1Y
  28.  
  29. Cart2Polar        :        This event converts cartesian to polar coordinates.
  30.                                                                                                 Input     - Trig1X, Trig1Y, 
  31.                                     Trig2X, Trig2Y
  32.                                                                                                 Output - TrigAngle, TrigDistance
  33.  
  34. Deg2Rad                        :        This event converts Degrees to Radians.
  35.                                                                                                 Input     - TrigInput
  36.                                                                                                 Output - TrigOutput
  37.  
  38. Rad2Deg                        :        This event converts Radians to Degrees.
  39.                                                                                                 Input     - TrigInput
  40.                                                                                                 Output - TrigOutput
  41.  
  42. Sqrt                                                    :        This event performs the Square root operation.
  43.                                                                                                 Input     - TrigInput
  44.                                                                                                 Output - TrigOutput
  45.  
  46. Modulus                                                :        This event performs Output = Input MOD TrigMod.
  47.                                                                                                 Input     - TrigInput TrigMod
  48.                                                                                                 Output - TrigOutput
  49.  
  50. [Parameters]
  51.  
  52. [Frame Loaded]
  53. GlobalVars TrigMod
  54. TrigMod = 360 //default value for angles.
  55. SpriteOfID($ThisSpriteID).SetVisible(False)
  56. SpriteOfID($ThisSpriteID).moveto(-1000,-1000)//off stage!
  57.  
  58. [200000 Trig]
  59. GlobalVars TrigAngle, sine, cosine, tangent
  60. //This function calculates the standard 3 trig functions sine,cosine and tangent
  61. //It does so by rotating the sprite and comparing the new width and height.
  62. Stretch(-200,-200,-100,-200,-100,-199,-200,-199)
  63. Rotate(TrigAngle)
  64. sine = (SecondCornerY-FirstCornerY)/100
  65. cosine = (SecondCornerx-FirstCornerx)/100
  66. IF (cosine = 0) //the tangent is infinite here! Don't divide by zero.
  67.     tangent = 999999
  68. ELSE
  69.     tangent =  sine/cosine
  70. ENDIF
  71.  
  72. [200001 MP_InvTan]
  73. GlobalVars TrigAngle, Trig1X, Trig1Y, Trig2X, Trig2Y
  74. SpriteVars MP_dir2point MP_point1x MP_point1y MP_point2x MP_point2y MP_tx MP_p45 MP_Tinverted MP_Tdiffx MP_Tdiffy MP_Tanglesign MP_Tanglepolarity
  75. //This is the inverse tangent, basically a standard coordinate system arctangent
  76. MP_Tdiffx = (MP_point1x-MP_point2x)
  77. MP_Tdiffy = (MP_point1y-MP_point2y)
  78. MP_tx = ABS(MP_Tdiffx)/ ABS(MP_Tdiffy)  
  79. MP_p45 = 0
  80. IF ( MP_tx > 0.65 AND MP_tx < 1.6)
  81.     Stretch(MP_point1x,MP_point1y,MP_point2x,MP_point1y,MP_point2x,MP_point2y,MP_point1x,MP_point2y)  
  82.     Rotate(45)
  83.     MP_Tdiffx = FirstCornerX - ThirdCornerX
  84.     MP_Tdiffy = FirstCornerY - ThirdCornerY
  85.     MP_p45 = -45
  86. ENDIF
  87.  
  88.  
  89. IF (ABS(MP_Tdiffx) < ABS(MP_Tdiffy))
  90.     MP_Tinverted = 90
  91.     MP_Tanglesign = -1    
  92.     MP_tx = MP_Tdiffx/MP_Tdiffy
  93.     IF (MP_Tdiffy>1) 
  94.         MP_Tanglepolarity = 0
  95.     ELSE
  96.         MP_Tanglepolarity = 180
  97.     ENDIF        
  98. ELSE
  99.     MP_Tanglesign = 1    
  100.     MP_Tinverted = 0
  101.     MP_tx = MP_Tdiffy/MP_Tdiffx
  102.     IF (MP_Tdiffx<1) 
  103.         MP_Tanglepolarity = 0
  104.     ELSE
  105.         MP_Tanglepolarity = 180
  106.     ENDIF           
  107. ENDIF    
  108. //Above are all correction factors for turning a single quadrant function into a 4 quad function.
  109. //Below is the actual atan calculation as a series:
  110. MP_dir2point=MP_tx*(1+MP_tx*MP_tx*(-1/3+MP_tx*MP_tx*(1/5+MP_tx*MP_tx*(-1/7+MP_tx*MP_tx*(1/9-MP_tx*MP_tx/11)))))
  111. TrigAngle = MP_Tanglesign*(MP_dir2point*180/3.1415926 + MP_Tinverted) - MP_Tanglepolarity + MP_p45
  112.  
  113.  
  114. [200002 ArcTan]
  115. GlobalVars TrigInput, TrigOutput, TrigAngle
  116. SpriteVars MP_point1x MP_point1y MP_point2x MP_point2y MP_tx
  117. //Set up the tangent ratio
  118. MP_tx = TrigInput 
  119. MP_point1x = -100
  120. MP_point1y = -100
  121. IF(ABS(MP_tx) < 1)
  122.     MP_point2x = -100 - 100*MP_tx
  123.     MP_point2y = -100 - 100
  124. ELSE
  125.     MP_point2x = -100 -100
  126.     MP_point2y = -100 - 100/MP_tx
  127. ENDIF
  128. ExecuteEvent(200001)//Calculate the inverse Tangent.
  129. TrigOutput = -(TrigAngle + 90) //correct for an inverted coordinate system.
  130.  
  131. [200003 Cart2Polar]
  132. GlobalVars TrigAngle, TrigDistance, Trig1X, Trig1Y, Trig2X, Trig2Y
  133. SpriteVars  MP_Tinverted, MP_Tdiffx, MP_Tdiffy, MP_Tanglesign, MP_Tanglepolarity, MP_point1x MP_point1y MP_point2x MP_point2y 
  134. //This function returns the vector components (TrigAngle, TrigDistance) of 
  135. //Trig2 relative to Trig1.
  136.  
  137. MP_point1x = Trig1X
  138. MP_point1y = Trig1Y
  139. MP_point2x =Trig2X
  140. MP_point2y =Trig2Y
  141.  
  142. ExecuteEvent(200001) //Calculate the inverse Tangent.
  143.  
  144. IF (TrigAngle = -180)
  145.     TrigDistance = Trig1X - Trig2X
  146. ELSE    
  147.     //Let the sprite calculate the squareroot for us since we know the angle already
  148.     Stretch(Trig1X,Trig1Y,Trig2X,Trig1Y,Trig2X,Trig2Y,Trig1X,Trig2Y)    
  149.     Rotate(-TrigAngle)
  150.     TrigDistance=BoundsRight-BoundsLeft         
  151. ENDIF    
  152.  
  153.  
  154. [200004 Polar2Cart]
  155. GlobalVars TrigAngle, TrigDistance, Trig1X, Trig1Y, sine, cosine
  156.  
  157. ExecuteEvent(200000) //The Trig function
  158. Trig1X = TrigDistance * cosine
  159. Trig1Y = TrigDistance * sine
  160.  
  161. [200014 Deg2Rad]
  162. GlobalVars TrigInput, TrigOutput
  163.  
  164. TrigOutput = TrigInput * pi / 180
  165.  
  166.  
  167. [200005 Rad2Deg]
  168. GlobalVars TrigInput, TrigOutput
  169.  
  170. TrigOutput = TrigInput * 180 / pi
  171.  
  172.  
  173. [200006 Sqrt]
  174. GlobalVars TrigInput, TrigOutput
  175. SpriteVars MP_tx, MP_rootscale
  176. //Squareroot function for LiveStage. Uses a folded series calculation developed by Matthew Peterson.
  177. MP_tx = ABS(TrigInput)
  178. MP_rootscale = 1
  179.  
  180. IF (MP_tx = 0)
  181.     TrigOutput = 0
  182. ELSE
  183.     //only one WHILE will be called!
  184.     WHILE (MP_tx < 0.4) 
  185.         MP_tx = MP_tx*4
  186.         MP_rootscale = MP_rootscale/2
  187.     ENDWHILE
  188.     
  189.     WHILE (MP_tx > 1.6)
  190.         MP_tx = MP_tx/4
  191.         MP_rootscale = MP_rootscale*2
  192.     ENDWHILE
  193.     MP_tx = MP_tx - 1
  194.     TrigOutput = (1+MP_tx*(0.5+MP_tx*(-1/8+MP_tx*(1/16+MP_tx*(-5/128+0.02734*MP_tx)))))*MP_rootscale
  195. ENDIF
  196.  
  197.  
  198. [200007 Modulus]
  199. GlobalVars TrigInput TrigMod TrigOutput
  200. // The Modulus function. Performs TrigOutput = TrigInput MOD TrigMod
  201. // LiveStage only has a REM function. This is the same as a MOD function
  202. // for positive values, but differs for negative values. For instance,
  203. // -90 rem 360 = -90
  204. // -90 mod 360 = 270
  205. // For angles, the MOD function is what is needed.
  206. TrigOutput = (TrigMod+(TrigInput rem TrigMod)) rem TrigMod 
  207.  
  208.  
  209.